package com.photoeditor.demo.ui.widget;

import android.animation.ValueAnimator;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Rect;
import android.util.AttributeSet;
import android.view.animation.LinearInterpolator;
import android.widget.RelativeLayout;


/**
 * Created by winn on 17/5/7.
 */

public class ClipAnimationRelativeLayout extends RelativeLayout {

    private ValueAnimator mAppearAnimator;
    private ValueAnimator mDisappearAnimator;

    private Rect mClipRect = new Rect();
    private int mClipH;

    public ClipAnimationRelativeLayout(Context context) {
        super(context);
    }

    public ClipAnimationRelativeLayout(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public ClipAnimationRelativeLayout(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    public void startAppearAnimation(int during) {
        if (isAnimationing()) {
            return;
        }
        mClipRect.set(0, 0, getWidth(), getHeight());
        mAppearAnimator = ValueAnimator.ofFloat(0, getHeight());
        mClipH = 0;
        mAppearAnimator.setDuration(during);
        mAppearAnimator.setInterpolator(new LinearInterpolator());
        mAppearAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator animation) {
                float value = (float) animation.getAnimatedValue();
                mClipH = (int) value;
                invalidate();
            }
        });
        mAppearAnimator.start();
    }

    public void startDisappearAnimation(int during) {
        if (isAnimationing()) {
            return;
        }
        mClipRect.set(0, 0, getWidth(), getHeight());
        mClipH = getHeight();
        mDisappearAnimator = ValueAnimator.ofFloat(getHeight(), 0);
        mDisappearAnimator.setDuration(during);
        mDisappearAnimator.setInterpolator(new LinearInterpolator());
        mDisappearAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator animation) {
                float value = (float) animation.getAnimatedValue();
                mClipH = (int) value;
                invalidate();
            }
        });
        mDisappearAnimator.start();
    }

    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);
        if (!isAnimationing()) {
            mClipH = h;
        }
    }

    private boolean isAnimationing() {
        return (mAppearAnimator != null && mAppearAnimator.isRunning())
                || (mDisappearAnimator != null && mDisappearAnimator.isRunning());
    }

//    @Override
//    public void draw(Canvas canvas) {
//        canvas.save();
//        if (mClipH != getHeight()) {
//            mClipRect.bottom = mClipH;
//            canvas.clipRect(mClipRect);
//        }
//        super.draw(canvas);
//        canvas.restore();
//    }

    @Override
    protected void dispatchDraw(Canvas canvas) {
        if (mClipH != getHeight()) {
            mClipRect.bottom = mClipH;
            canvas.clipRect(mClipRect);
        }
        super.dispatchDraw(canvas);
    }

    @Override
    protected void onDetachedFromWindow() {
        super.onDetachedFromWindow();
        destroyAnimation();
    }

    private void destroyAnimation() {
        if (mAppearAnimator != null && mAppearAnimator.isRunning()) {
            mAppearAnimator.cancel();
        }
        if (mDisappearAnimator != null && mDisappearAnimator.isRunning()) {
            mDisappearAnimator.cancel();
        }
    }
}
